package com.cskaoyan.controller;

import com.cskaoyan.bean.BO.AdminAdminCreateBO;
import com.cskaoyan.bean.PO.MarketAdmin;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminAdminService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName AdminAdminController
 * @Description
 * @Author Lxz
 * @Date 2022-8-12 21:23
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    AdminAdminService adminAdminService;


    @RequiresPermissions("admin:admin:list")
    @RequestMapping("list")
    public BaseRespVo list(String username,BasePageInfo info) {
        CommonData<MarketAdmin> list = adminAdminService.list(username,info);
        return BaseRespVo.ok(0,list,"成功");
    }

    @RequiresPermissions("admin:admin:create")
    @RequestMapping("create")
    public BaseRespVo createNAdminLog(@RequestBody MarketAdmin marketAdmin) {
        String username = marketAdmin.getUsername();
        String password = marketAdmin.getPassword();

        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("logType", 1);
        session.setAttribute("logAction", "添加管理员");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult",username);
        session.setAttribute("logComment","");

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return BaseRespVo.custom(401,null,"管理员账户与密码不能为空");
        }
        if (username.length() < 6 ) {
            return BaseRespVo.custom(601, null, "管理员名称长度不能小于6");
        }
        if (password.length() < 6 ) {
            return BaseRespVo.custom(602, null, "管理员密码长度不能小于6");
        }
        if (password.length() > 60 || username.length() > 60) {
            return BaseRespVo.custom(502,null,"输入账户或密码过长，请重新输入！");
        }

        List<MarketAdmin> marketAdmins = adminAdminService.judgeRepeat(marketAdmin);
        if (marketAdmins.size() != 0  ) {
            return BaseRespVo.custom(602,null,"管理员已经存在");
        }
        MarketAdmin marketAdmin1 = adminAdminService.create(marketAdmin);



        return BaseRespVo.ok(0,marketAdmin1,"成功");
    }

    @RequiresPermissions("admin:admin:update")
    @RequestMapping("update")
    public BaseRespVo updateNAdminLog(@RequestBody MarketAdmin marketAdmin) {
        String username = marketAdmin.getUsername();
        String password = marketAdmin.getPassword();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return BaseRespVo.custom(401,null,"管理员账户与密码不能为空");
        }
        if (username.length() < 6 ) {
            return BaseRespVo.custom(601, null, "管理员名称长度不能小于6");
        }
        if (password.length() < 6 ) {
            return BaseRespVo.custom(602, null, "管理员密码长度不能小于6");
        }
        if (password.length() > 60 || username.length() > 60) {
            return BaseRespVo.custom(502,null,"输入账户或密码过长，请重新输入！");
        }

        List<MarketAdmin> marketAdmins = adminAdminService.judgeRepeat(marketAdmin);
        if (marketAdmins.size() == 1 && !username.equals(marketAdmins.get(0).getUsername())) {
            return BaseRespVo.custom(602, null, "管理员已经存在");
        }

        MarketAdmin marketAdmin1 = adminAdminService.update(marketAdmin);

        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("logType", 1);
        session.setAttribute("logAction", "编辑管理员");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult",marketAdmin1.getUsername());
        session.setAttribute("logComment","");

        return BaseRespVo.ok(0,marketAdmin1,"成功");
    }

    @RequiresPermissions("admin:admin:delete")
    @RequestMapping("delete")
    public BaseRespVo deleteNLog(@RequestBody MarketAdmin marketAdmin) {
        /*
         * todo-Lxz 魔法值→配置文件
         * @Date  15:44
         **/
        if ("admin123".equals(marketAdmin.getUsername())) {
            return BaseRespVo.invalidParameter("该管理员不可删除");
        }
        adminAdminService.delete(marketAdmin);

        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("logType", 1);
        session.setAttribute("logAction", "删除管理员");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult",marketAdmin.getUsername());
        session.setAttribute("logComment","");

        return BaseRespVo.ok(0,null,"成功");
    }

}
